home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / ghostscript / src / gxhint1.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  7KB  |  198 lines

  1. /* Copyright (C) 1990, 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gxhint1.c */
  20. /* Font level hints for Type 1 fonts */
  21. #include "gx.h"
  22. #include "gserrors.h"
  23. #include "gxarith.h"
  24. #include "gxfixed.h"
  25. #include "gxmatrix.h"
  26. #include "gzstate.h"
  27. #include "gzdevice.h"            /* for gxchar */
  28. #include "gxdevmem.h"            /* ditto */
  29. #include "gxchar.h"
  30. #include "gxfont.h"
  31. #include "gxtype1.h"
  32.  
  33. /* ------ Initialization ------ */
  34.  
  35. typedef zone_table(1) a_zone_table;
  36. typedef stem_table(1) a_stem_table;
  37. private void near
  38.   compute_snaps(P6(const gs_matrix_fixed *, const a_stem_table *,
  39.     stem_snap_table *, int, int, const char *));
  40. private alignment_zone *near
  41.   compute_zones(P6(const gs_matrix_fixed *, const font_hints *,
  42.     const a_zone_table *, const a_zone_table *, alignment_zone *, int));
  43. private void near
  44.   transform_zone(P4(const gs_matrix_fixed *, const font_hints *,
  45.     const float *, alignment_zone *));
  46.  
  47. /* Compute the font-level hints from the font and the matrix. */
  48. /* We should cache this with the font/matrix pair.... */
  49. void
  50. compute_font_hints(font_hints *pfh, const gs_matrix_fixed *pmat,
  51.   int log2_scale, const gs_type1_data *pdata)
  52. {    alignment_zone *zp = &pfh->a_zones[0];
  53.     set_pixel_scale(&pfh->scale, log2_scale);
  54.     /* Figure out which hints, if any, to use, */
  55.     /* and the orientation of the axes. */
  56.     pfh->axes_swapped = pfh->x_inverted = pfh->y_inverted = 0;
  57.     pfh->use_x_hints = pfh->use_y_hints = 0;
  58.     if ( is_fzero(pmat->xy) )
  59.         pfh->y_inverted = is_fneg(pmat->yy),
  60.         pfh->use_y_hints = 1;
  61.     else if ( is_fzero(pmat->xx) )
  62.         pfh->y_inverted = is_fneg(pmat->xy),
  63.         pfh->axes_swapped = 1,
  64.         pfh->use_y_hints = 1;
  65.     if ( is_fzero(pmat->yx) )
  66.         pfh->x_inverted = is_fneg(pmat->xx),
  67.         pfh->use_x_hints = 1;
  68.     else if ( is_fzero(pmat->yy) )
  69.         pfh->x_inverted = is_fneg(pmat->yx),
  70.         pfh->axes_swapped = 1,
  71.         pfh->use_x_hints = 1;
  72.     if_debug6('y', "[y]ctm=[%g %g %g %g %g %g]\n",
  73.           pmat->xx, pmat->xy, pmat->yx, pmat->yy,
  74.           pmat->tx, pmat->ty);
  75.     if_debug5('y', "[y]swapped=%d, x/y_hints=%d,%d, x/y_inverted=%d,%d\n",
  76.           pfh->axes_swapped, pfh->use_x_hints, pfh->use_y_hints,
  77.           pfh->x_inverted, pfh->y_inverted);
  78.     /* Transform the actual hints. */
  79.     pfh->snap_h.count = 0;
  80.     if ( pfh->use_x_hints )
  81.     {    compute_snaps(pmat, (const a_stem_table *)&pdata->StdHW,
  82.                   &pfh->snap_h, 0, pfh->axes_swapped, "h");
  83.         compute_snaps(pmat, (const a_stem_table *)&pdata->StemSnapH,
  84.                   &pfh->snap_h, 0, pfh->axes_swapped, "h");
  85.     }
  86.     pfh->snap_v.count = 0;
  87.     if ( pfh->use_y_hints )
  88.     {    gs_fixed_point vw;
  89.         fixed *vp = (pfh->axes_swapped ? &vw.x : &vw.y);
  90.         /* Convert blue parameters to device pixels. */
  91.         gs_distance_transform2fixed(pmat, 0.0,
  92.                         (float)pdata->BlueFuzz, &vw);
  93.         pfh->blue_fuzz = any_abs(*vp);
  94.         gs_distance_transform2fixed(pmat, 0.0, 1.0, &vw);
  95.         pfh->suppress_overshoot =
  96.           (any_abs(*vp) >> log2_scale) < pdata->BlueScale;
  97.         gs_distance_transform2fixed(pmat, 0.0, pdata->BlueShift, &vw);
  98.         pfh->blue_shift = any_abs(*vp);
  99.         /* Tweak up blue_shift if it is less than half a pixel. */
  100.         /* See the discussion of BlueShift in section 5.7 of */
  101.         /* "Adobe Type 1 Font Format." */
  102.         if ( pfh->blue_shift < pfh->scale.half )
  103.             pfh->blue_shift = pfh->scale.half;
  104.         if_debug6('y', "[y]blue_fuzz=%d->%g, blue_scale=%g, blue_shift=%g->%g, sup_ov=%d\n",
  105.               pdata->BlueFuzz, fixed2float(pfh->blue_fuzz),
  106.               pdata->BlueScale,
  107.               pdata->BlueShift, fixed2float(pfh->blue_shift),
  108.               pfh->suppress_overshoot);
  109.         zp = compute_zones(pmat, pfh,
  110.                    (const a_zone_table *)&pdata->BlueValues,
  111.                    (const a_zone_table *)&pdata->FamilyBlues,
  112.                    zp, 1);
  113.         zp = compute_zones(pmat, pfh,
  114.                    (const a_zone_table *)&pdata->OtherBlues,
  115.                    (const a_zone_table *)&pdata->FamilyOtherBlues,
  116.                    zp, max_OtherBlues);
  117.         compute_snaps(pmat, (const a_stem_table *)&pdata->StdVW,
  118.                   &pfh->snap_v, 1, !pfh->axes_swapped, "v");
  119.         compute_snaps(pmat, (const a_stem_table *)&pdata->StemSnapV,
  120.                   &pfh->snap_v, 1, !pfh->axes_swapped, "v");
  121.     }
  122.     pfh->a_zone_count = zp - &pfh->a_zones[0];
  123. }
  124.  
  125. /* Transform one set of stem snap widths. */
  126. private void near
  127. compute_snaps(const gs_matrix_fixed *pmat, const a_stem_table *pst,
  128.   stem_snap_table *psst, int from_y, int to_y, const char *tname)
  129. {    gs_fixed_point wxy;
  130.     fixed *wp = (to_y ? &wxy.y : &wxy.x);
  131.     int i;
  132.     int j = psst->count;
  133.     for ( i = 0; i < pst->count; i++, j++ )
  134.     {    float w = pst->values[i];
  135.         if ( from_y )
  136.             gs_distance_transform2fixed(pmat, 0.0, w, &wxy);
  137.         else
  138.             gs_distance_transform2fixed(pmat, w, 0.0, &wxy);
  139.         psst->data[j] = any_abs(*wp);
  140.         if_debug3('y', "[y]snap_%s[%d]=%g\n", tname, j,
  141.               fixed2float(psst->data[j]));
  142.     }
  143.     psst->count = j;
  144. }
  145.  
  146. /* Compute the alignment zones for one set of 'blue' values. */
  147. private alignment_zone *near
  148. compute_zones(const gs_matrix_fixed *pmat, const font_hints *pfh,
  149.   const a_zone_table *blues, const a_zone_table *family_blues,
  150.   alignment_zone *zp, int bottom_count)
  151. {    int i;
  152.     fixed fuzz = pfh->blue_fuzz;
  153.     int inverted =
  154.         (pfh->axes_swapped ? pfh->x_inverted : pfh->y_inverted);
  155.     for ( i = 0; i < blues->count; i += 2, zp++ )
  156.     {    const float *vp = &blues->values[i];
  157.         zp->is_top_zone = i >> 1 >= bottom_count;
  158.         transform_zone(pmat, pfh, vp, zp);
  159.         if_debug5('y', "[y]blues[%d]=%g,%g -> %g,%g\n",
  160.               i >> 1, vp[0], vp[1],
  161.               fixed2float(zp->v0), fixed2float(zp->v1));
  162.         if ( i < family_blues->count )
  163.         {    /* Check whether family blues should supersede. */
  164.             alignment_zone fz;
  165.             const float *fvp = &family_blues->values[i];
  166.             fixed diff;
  167.             transform_zone(pmat, pfh, fvp, &fz);
  168.             if_debug5('y', "[y]f_blues[%d]=%g,%g -> %g,%g\n",
  169.                   i >> 1, fvp[0], fvp[1],
  170.                   fixed2float(fz.v0), fixed2float(fz.v1));
  171.             diff = (zp->v1 - zp->v0) - (fz.v1 - fz.v0);
  172.             if ( diff > -pfh->scale.unit && diff < pfh->scale.unit )
  173.                 zp->v0 = fz.v0, zp->v1 = fz.v1;
  174.         }
  175.         /* Compute the flat position, and add the fuzz. */
  176.         if ( (inverted ? zp->is_top_zone : !zp->is_top_zone) )
  177.             zp->flat = zp->v1, zp->v0 -= fuzz;
  178.         else
  179.             zp->flat = zp->v0, zp->v1 += fuzz;
  180.     }
  181.     return zp;
  182. }
  183.  
  184. /* Transform a single alignment zone to device coordinates, */
  185. /* taking axis swapping into account. */
  186. private void near
  187. transform_zone(const gs_matrix_fixed *pmat, const font_hints *pfh,
  188.   const float *vp, alignment_zone *zp)
  189. {    gs_fixed_point p0, p1;
  190.     fixed v0, v1;
  191.     gs_point_transform2fixed(pmat, 0.0, vp[0], &p0);
  192.     gs_point_transform2fixed(pmat, 0.0, vp[1], &p1);
  193.     if ( pfh->axes_swapped ) v0 = p0.x, v1 = p1.x;
  194.     else v0 = p0.y, v1 = p1.y;
  195.     if ( v0 <= v1 ) zp->v0 = v0, zp->v1 = v1;
  196.     else zp->v0 = v1, zp->v1 = v0;
  197. }
  198.